﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;

namespace ISolutions.SharePoint.MuiManager
{
    public class MuiResource
    {
        Dictionary<int, string> resources = new Dictionary<int, string>();

        #region Properties for Grid Binding
        /// <summary>
        /// German 
        /// </summary>
        public string Res1031
        {
            get
            {
                return GetResource(1031);
            }
        }

        /// <summary>
        /// English
        /// </summary>
        public string Res1033
        {
            get
            {
                return GetResource(1033);
            }
        }

        /// <summary>
        /// French
        /// </summary>
        public string Res1036
        {
            get
            {
                return GetResource(1036);
            }
        }

        /// <summary>
        /// Spanish
        /// </summary>
        public string Res3082
        {
            get
            {
                return GetResource(3082);
            }
        }

        #endregion

        /// <summary>
        /// Gets or sets the key of the current resource
        /// </summary>
        public string Key
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the SPResourceType
        /// </summary>
        public SPResourceType Type
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new MuiResource
        /// </summary>
        /// <param name="reference">key of the resource</param>
        /// <param name="resourceValue">value of the resource</param>
        public MuiResource(string reference)
        {
            this.Key = reference;
        }

        /// <summary>
        /// Initializes a new MuiResource
        /// </summary>
        /// <param name="reference">key of the resource</param>
        /// <param name="resourceValue">value of the resource</param>
        /// <param name="type">type of resource (Single-/Multiline)</param>
        /// <param name="saveInAllLanguages">if true a new resource will be saved in all installed languages</param>
        public MuiResource(string reference, SPResourceType type)
        {
            this.Key = reference;
            this.Type = type;
        }

        public void InitializeResource(SPUserResource userResource)
        {
            foreach (MuiLanguage language in LanguageService.Instance.GetLanguages(SPContext.Current.Web))
            {
                resources.Add(language.LanguageCode, userResource.GetValueForUICulture(language.CultureInfo));
            }
        }

        /// <summary>
        /// Gets the resource.
        /// </summary>
        /// <param name="languageCode">The language code.</param>
        /// <returns></returns>
        public string GetResource(int languageCode)
        {
            if (resources.ContainsKey(languageCode))
            {
                return resources[languageCode];
            }
            else
            {
                return String.Empty;
            }
        }

        /// <summary>
        /// Sets the resource.
        /// </summary>
        /// <param name="languageCode">The language code.</param>
        /// <param name="value">The value.</param>
        public void SetResource(int languageCode, string value)
        {
            if (resources.ContainsKey(languageCode))
            {
                resources[languageCode] = value;
            }
        }



        /// <summary>
        /// Saves the current resource to the specified SPWeb
        /// </summary>
        /// <param name="web"></param>
        public void Save(SPWeb web)
        {
            if (!MuiResourceService.Exists(web, this.Key))
            {
                try
                {
                    SPUserResource res = new SPUserResource(this.Key, this.GetResource(LanguageService.Instance.DefaultLanguage(web).LanguageCode), this.Type);

                    foreach (MuiLanguage lang in LanguageService.Instance.GetLanguages(web))
                    {
                        res.SetValueForUICulture(lang.CultureInfo, GetResource(lang.LanguageCode));
                    }

                    web.UserResources.Add(res);
                    web.Update();
                }
                catch (Exception)
                {
                    // TODO: Exception Handling
                }
            }
            else
            {
                this.Update(web);
            }
        }



        /// <summary>
        /// Updates the current resource on the specified web.
        /// </summary>
        /// <param name="web"></param>
        private void Update(SPWeb web)
        {
            SPUserResource resource = MuiResourceService.Find(web, this.Key);

            if (resource != null)
            {
                foreach (MuiLanguage lang in LanguageService.Instance.GetLanguages(web))
                {
                    resource.SetValueForUICulture(lang.CultureInfo, GetResource(lang.LanguageCode));
                }
                resource.Update();
            }
        }

    }
}

